﻿using Mondol.DapperPoco;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure;
using System.Data.Metadata.Edm;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var masDb = new MasterDbContext();
            var userInfo = masDb.FirstOrDefault<USER_INFO_MST>("select * from USER_INFO_MST");
            Console.WriteLine("姓名:{0}",userInfo.UR_NAME);

            USER_INFO_MST us = new USER_INFO_MST
            {
                UR_ID = "0001"
            };
            
            KS_SMHS_DETAIL_TBL ks = new KS_SMHS_DETAIL_TBL() { 
                KSX_ID=0,
                KSX_RU_ID="2001",
                KSX_NOTE_ID=20020,
                KSX_RU_TYPE="2"
            };

            dynamic list = new
            {
                us,
                KsDetail = ks
            };

            
            string res = masDb.ExecuteScalar<string>("select UR_NAME from USER_INFO_MST where UR_ID=@p0", new { p0 = "0001" });

            dynamic obj = JObject.FromObject(us);
            obj.Add("KsDetail", JToken.FromObject(ks));
            string re = JsonConvert.SerializeObject(obj);

            var ss = obj.KsDetail.KSX_RU_ID.Value;
           
            Type t = typeof(USER_INFO_MST);

            var colNames = t.GetProperties().Where(prop => prop.GetValue(us) != null && !string.IsNullOrEmpty(prop.GetValue(us).ToString().Trim())
                                                      && (prop.PropertyType == typeof(decimal) ? Convert.ToDecimal(prop.GetValue(us)) > 0 : true)).ToList();
                //.Select(p => eTabInfo.Columns.First(p1 => p1.Property.Name == p.Name).ColumnName).ToList();



            var cs = t.GetProperties().Where(prop => prop.PropertyType == typeof(decimal));
          
            foreach (var p in t.GetProperties())
            {
                int s = p.GetAccessors().Where(a => a.IsVirtual).Count();

                var s1 = p.GetCustomAttributes<JsonIgnoreAttribute>();
                var s2 = p.GetCustomAttributes<DatabaseGeneratedAttribute>();
                int s22 = s2.Count();
                var s3 = p.GetCustomAttributes<KeyAttribute>();
                var ssss = p.CustomAttributes.Any(a=>a.AttributeType==typeof(DatabaseGeneratedAttribute));
                ssss = p.CustomAttributes.Any(a => a.AttributeType == typeof(KeyAttribute));
                int s33 = s3.Count();
                var s4 = p.GetCustomAttributes<KeyAttribute>();
                int s44 = s4.Count();
                //|| a.Match(typeof(DatabaseGeneratedAttribute))
                //TypeId==typeof(DatabaseGeneratedAttribute)
                if (s1.Count()>0)
                {

                }

                masDb.BeginTransaction();
                if (s2.Count() > 0)
                {

                }

                Console.WriteLine("属性名：{0} {1}", p.Name, s);
            }

            var depart = masDb.FetchAll<DEPARTMENT_INFO_MST>();

            ICollection<string> columnNames = new List<string>{
                "helo","perter","age","address"
            };

            var where = string.Join(" and ", columnNames.Select(p => string.Format("{0} = @{1}", p, p)));
            if (!string.IsNullOrEmpty(where))
            {
                where = " where "+where;
            }

            where = LambdaToSql.GetWhereByLambda<USER_INFO_MST>(a => a.UR_ID == "0001");

            int aa = masDb.Save(ks, primaryKeyName: "KSX_ID");
           
            //string str = checkIfNull<USER_INFO_MST>.Check(us);

            //string title = "";
            //var sb = new SqlBuilder();
            //sb.Append("select * from T_Article");
            //if (!string.IsNullOrEmpty(title))
            //    sb.Append("where Title=@p0", title);

            Paged<USER_INFO_MST> paged = masDb.Paged<USER_INFO_MST>(1, 10, "select * from USER_INFO_MST order by ur_id");
           

            //var sql = sb.Build();
            //var articles = masterDb.Fetch<Article>(sql.Statement, sql.Parameters);

            var userInfo1 = masDb.FetchAll<USER_INFO_MST>(us);
            userInfo1 = masDb.FetchByProperty<USER_INFO_MST>(us, a=> new { a.UR_ID,a.UR_NAME});
            var userList = masDb.FetchAll<USER_INFO_MST>();

            foreach (var item in userList)
            {
                Console.WriteLine(string.Format("ID:{0}  姓名:{1}", item.UR_ID, item.UR_NAME));
            }
            Console.ReadKey();
        }
    }

    public static class checkIfNull<T> where T:class
    {
        public static string Check(T t)
        {
            Type type = t.GetType();
            PropertyInfo[] props = type.GetProperties();
            return (props.Where(prop => prop.GetValue(t) != null && !string.IsNullOrEmpty(prop.GetValue(t).ToString().Trim()))
                .Select(prop=>string.Format("Parameter {0} can not be empty.",prop.Name))).FirstOrDefault();

        }
    }
}
